﻿using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools;
using Microsoft.Office.Tools.Ribbon;
using TShooter.TfsWord.AddIn.RibbonActions;
using TShooter.TfsWord.Resources;

namespace TShooter.TfsWord.AddIn
{
    public partial class TeamRibbon : OfficeRibbon
    {
        public TeamRibbon()
        {
            InitializeComponent();
            InitializeStrings();
        }

        private Window Window
        {
            get { return Context as Window; }
        }

        private void InitializeStrings()
        {
            TabTfsWord.Label = Strings.RibbonCaption;

            grpWorkItems.Label = Strings.RibbonGroupWorkItems;
            btnNewDocument.Label = Strings.RibbonButtonNewDocument;
            btnPublish.Label = Strings.RibbonButtonPublish;
            btnRefresh.Label = Strings.RibbonButtonRefresh;
            btnGetWorkItem.Label = Strings.RibbonButtonGetWorkItem;
            btnEditAreas.Label = Strings.EditAreasAndIterations;

            grpAutoLists.Label = Strings.RibbonGroupAutoLists;
            tgbRevisions.Label = Strings.RibbonButtonRevisions;
            tgbLinks.Label = Strings.RibbonButtonLinks;
            tgbAttachments.Label = Strings.RibbonButtonAttachments;

            grpTemplates.Label = Strings.RibbonGroupTemplates;
            galTemplates.Label = Strings.RibbonButtonTemplates;

            grpConnection.Label = Strings.RibbonGroupConnection;
            lblServerCaption.Label = Strings.RibbonLabelServer;
            lblProjectCaption.Label = Strings.RibbonLabelTeamProject;
            lblStatusCaption.Label = Strings.RibbonLabelStatus;
            lblServer.Label = Strings.RibbonLabelNotAvailable;
            lblProject.Label = Strings.RibbonLabelNotAvailable;
            lblStatus.Label = Strings.RibbonLabelNotAvailable;

            grpHelp.Label = Strings.RibbonGroupHelp;
            btnHelp.Label = Strings.RibbonButtonProductHelp;
            btnProcessGuidance.Label = Strings.RibbonButtonProcessGuidance;
        }

        private void btnNewDocument_Click(object sender, RibbonControlEventArgs e)
        {
            StartNewDocumentWizard();
        }

        private void grpWorkItems_DialogLauncherClick(object sender, RibbonControlEventArgs e)
        {
            SetTaskPaneVisibility<WorkItemFieldList>((Window) e.Control.Context, true);
        }

        private void grpConnection_DialogLauncherClick(object sender, RibbonControlEventArgs e)
        {
        }

        /// <summary>
        /// Finds all custom task panes that contains a user control of <paramref name="T"/> associated to <paramref name="window"/> and sets their Visible properties.
        /// </summary>
        /// <param name="window">Target window.</param>
        /// <param name="T">Type of the user control.</param>
        private static void SetTaskPaneVisibility<T>(Window window)
            where T : UserControl
        {
            foreach (CustomTaskPane taskPane in Globals.UIManager[window].CustomTaskPanes.WithControl<T>())
            {
                taskPane.Visible = !taskPane.Visible;
            }
        }

        /// <summary>
        /// Finds all custom task panes that contains a user control of <paramref name="T"/> associated to <paramref name="window"/> and sets their Visible properties.
        /// </summary>
        /// <param name="window">Target window.</param>
        /// <param name="T">Type of the user control.</param>
        /// <param name="visible">New Visible value.</param>
        private static void SetTaskPaneVisibility<T>(Window window, bool visible)
            where T : UserControl
        {
            foreach (CustomTaskPane taskPane in Globals.UIManager[window].CustomTaskPanes.WithControl<T>())
            {
                taskPane.Visible = visible;
            }
        }

        private void StartNewDocumentWizard()
        {
            (new StartNewDocument()).Execute(Window);
        }

        public void SyncToDocument(Document document)
        {
            Globals.CurrentDocument = new TeamFoundation.Document(document);
            SyncControls();
        }

        private void SyncControls()
        {
            if (Globals.CurrentDocument == null)
            {
                return;
            }

            Uri teamProjectCollectionUri = Globals.CurrentDocument.TeamProjectCollectionUri;

            lblServer.Label = teamProjectCollectionUri == null
                                  ? Strings.RibbonLabelNotAvailable
                                  : teamProjectCollectionUri.Host + teamProjectCollectionUri.AbsolutePath;
            lblProject.Label = Globals.CurrentDocument.TeamProject ?? Strings.RibbonLabelNotAvailable;
            lblStatus.Label = Globals.CurrentDocument.LastStatus.ToString();
        }
    }
}